package com.pcitc.paas.feign.demo.api.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * description: <br>
 * createTime: 2021/6/21 17:45 <br>
 *
 * @author zzh
 */
public class Model2MapUtils {
    private static Logger logger = LoggerFactory.getLogger(Model2MapUtils.class);
    public static final String SETTER_PREFIX = "set";
    public static final String GETTER_PREFIX = "get";

    public Model2MapUtils() {
    }

    public static <T> Map<String, Object> convertDto2Map(T t) {
        if (t == null) {
            return new HashMap();
        } else {
            Map<String, Object> result = new HashMap();
            Class<?> clazz = t.getClass();
            Method[] methods = clazz.getMethods();
            Method[] var4 = methods;
            int var5 = methods.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Method method = var4[var6];
                String name = method.getName();
                if (!"getClass".equals(name) && name.startsWith("get")) {
                    try {
                        String tempName = name.substring(3);
                        result.put(tempName.substring(0, 1).toLowerCase() + tempName.substring(1), method.invoke(t));
                    } catch (InvocationTargetException | IllegalAccessException var10) {
                        logger.error("convertDto2Map error");
                    }
                }
            }

            return result;
        }
    }

    public static <T> String convertDto2QueryString(T t) {
        return convertDto2Map(t).toString().replaceAll(",", "&");
    }
}